package com.goldsprite.microhorigame.screens.examples.ecsgfsmexamples;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.goldsprite.gameframeworks.assets.GameRes;
import com.goldsprite.gameframeworks.ecs.SpriteUtils;
import com.goldsprite.gameframeworks.ecs.component.AnimatorComponent;
import com.goldsprite.gameframeworks.ecs.component.SpriteComponent;
import com.goldsprite.gameframeworks.ecs.entity.GObject;
import com.goldsprite.gameframeworks.ecs.system.GameSystem;
import com.goldsprite.gameframeworks.log.Debug;
import com.goldsprite.gameframeworks.utils.Pivot;
import com.goldsprite.gameframeworks.utils.math.Vector2;
import com.goldsprite.microhorigame.ecs.EntityInputComponent;
import com.goldsprite.microhorigame.ecs.EntityPropsComponent;
import com.goldsprite.microhorigame.ecs.EntityFsmComponent;
import com.goldsprite.microhorigame.screens.basics.ExampleGScreen;

public class EcsGFsmExample extends ExampleGScreen {
	int worldViewWidth = 960, worldViewHeight = 540;
	int uiViewWidth = 960, uiViewHeight = 540;
	private FitViewport worldViewport, uiViewport;
	private OrthographicCamera worldCamera, uiCamera;

	private GObject player;
	private GameSystem gm;

	@Override
	public String getIntroduction() {
		return "ECS适配的GFsm实体状态机角色实例演示";
	}

	@Override
	public void create() {
		init();
		createGM();

		createPlayer();
	}
	private void init(){
		worldCamera = new OrthographicCamera();
		worldViewport = new FitViewport(worldViewWidth, worldViewHeight, worldCamera);
		worldCamera.setToOrtho(false, worldViewWidth, worldViewHeight);
		worldCamera.position.set(worldViewWidth / 2f, worldViewHeight / 2f, 0);
		worldCamera.update();

		uiCamera = new OrthographicCamera();
		uiViewport = new FitViewport(uiViewWidth, uiViewHeight, uiCamera);
		uiCamera.setToOrtho(false, uiViewWidth, uiViewHeight);
		uiCamera.position.set(uiViewWidth / 2f, uiViewHeight / 2f, 0);
		uiCamera.update();
	}

	private void createGM() {
		GameSystem.setViewport(worldViewport, uiViewport);
		gm = new GameSystem();
	}

	private void createPlayer() {
		Vector2 spawnPos = new Vector2(200, 130);
		float playerScl = 2f;

		player = new GObject(){{
			setName("Player");
			setTransform();
		}};
		Debug.logT("PlayerInfo", "玩家创建, 玩家名: %s", player.getName());
		player.transform.getPosition().set(spawnPos.x, spawnPos.y);
		spawnPos.close();
		player.transform.getScale().set(playerScl);

		SpriteComponent sprite = player.addComponent(SpriteComponent.class);
		sprite.setSpriteLayer("Entity");
		sprite.setRegion(GameRes.Boss_Sample);
		sprite.setPivot(Pivot.LeftDown);
		sprite.setOriginOffset(117, 16);
		sprite.setScale(1, 1);
		sprite.setColor(Color.WHITE);

		AnimatorComponent animator = player.addComponent(AnimatorComponent.class);
		SpriteUtils.setUVUnit(390, 184);
		animator.addAnim("Idle", new Animation<>(.15f, SpriteUtils.splitFrames(GameRes.Frost, 0, 6), Animation.PlayMode.LOOP));
		animator.addAnim("Move", new Animation<>(.2f, SpriteUtils.splitFrames(GameRes.Frost, 1, 4), Animation.PlayMode.LOOP));
		animator.addAnim("Attack", new Animation<>(.12f, SpriteUtils.splitFrames(GameRes.Frost, 3, 11), Animation.PlayMode.NORMAL));

		EntityInputComponent input = player.addComponent(EntityInputComponent.class);
		EntityPropsComponent props = player.addComponent(EntityPropsComponent.class);


		EntityFsmComponent fsmComp = player.addComponent(EntityFsmComponent.class);

//		EntityFsm fsm = new EntityFsm();
//		fsm.onInitializeStates(()-> {
//			fsm.addState(new EntityIdleState(), 0);
//			fsm.addState(new EntityMoveState(), 1);
//			fsm.addState(new EntityAttackState(), 2);
//		});
//		fsmComp.setFsm(fsm);
	}

	@Override
	public void render(float delta) {
		super.render(delta);

		worldViewport.apply();
		uiViewport.apply();

		gm.gameLoop(delta);

		Debug.infoT("PlayerInfo", "玩家位置: %s", player.transform.getPosition());
	}

	@Override
	public void resize(int width, int height) {
		worldViewport.update(width, height, true);
		uiViewport.update(width, height, true);
	}
}
